package com.wtwd.campus.receiver;

import com.alibaba.fastjson.JSONObject;
import com.wtwd.campus.annotation.MsgType;
import com.wtwd.campus.common.Constants;
import com.wtwd.campus.dao.CommandRecordMapper;
import com.wtwd.campus.dao.DeviceInfoMapper;
import com.wtwd.campus.entity.CommandRecord;
import com.wtwd.campus.entity.DeviceInfo;
import com.wtwd.campus.entity.MqMessage;
import com.wtwd.campus.entity.MqModeResult;
import com.wtwd.campus.h5_manager.entity.DeviceLog;
import com.wtwd.campus.h5_manager.service.DeviceLogService;
import com.wtwd.campus.utils.PushUtils;
import com.wtwd.campus.utils.ReceiverUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author mjy
 * @date 2021/07/02
 * @description
 */
@Service
@MsgType(value = Constants.SET_WORK_MODE)
public class ReceiveWorkModelHandler implements IReceiverHandler {

    @Resource
    private DeviceInfoMapper deviceInfoMapper;
    @Resource
    private CommandRecordMapper commandRecordMapper;
    @Autowired
    private ReceiverUtils receiverUtils;
    @Autowired
    private PushUtils pushUtils;
    @Autowired
    private DeviceLogService deviceLogService;

    @Override
    public void handlerMessage(MqMessage mqMessage) {
        MqModeResult mqModeResult = (MqModeResult) mqMessage.getObject();
        //根据流水号，imei号，和指令类型找到唯一的一条指令记录
        CommandRecord commandRecord = commandRecordMapper.getCommandRecordByCondition2(mqModeResult.getImei(), mqModeResult.getCommandNumber(), Constants.SET_WORK_MODE);
        if (commandRecord!=null){
            DeviceInfo deviceInfo = deviceInfoMapper.getDeviceInfoByImei(mqModeResult.getImei());
            //如果定位数据不为空的话
            if (mqModeResult.getMqLocationData() != null) {
                if (mqModeResult.getMqLocationData().getLatitude() != 0 && mqModeResult.getMqLocationData().getLongitude() != 0) {
                    //处理指令回复的时间
                    String deviceDate = mqModeResult.getDeviceDate();
                    String deviceTime = mqModeResult.getDeviceTime();
                    Long time = getTime(deviceDate, deviceTime);
                    //定位数据处理（保存 + 围栏判断
                    deviceInfo = receiverUtils.dealLocation(mqModeResult.getMqLocationData(),time,deviceInfo, 1);
                }
            }
            //修改设备的工作模式为设备回复的,和上报时间间隔
            deviceInfo.setWorkingPattern(mqModeResult.getMode());
            deviceInfo.setUpInterval(mqModeResult.getInterval());
            deviceInfo.setLastUpTime(System.currentTimeMillis());
            // 指令序列号一样，修改为空字符串
            if(mqModeResult.getCommandNumber().equalsIgnoreCase(deviceInfo.getCommandNumber())){
                deviceInfo.setCommandNumber("");
            }
            deviceInfoMapper.updateDeviceInfo(deviceInfo);
            //删除缓存中的指令
            String delKey = Constants.SET_WORK_MODE.concat("-").concat(mqModeResult.getCommandNumber());
            receiverUtils.deleteCommandInRedis(mqModeResult.getImei(), delKey);
            //将指令状态改为完成
            commandRecordMapper.completeCommand(commandRecord.getId(), System.currentTimeMillis());
            //存储设备接收日志
            DeviceLog deviceLog = new DeviceLog();
            deviceLog.setCommandNumber(mqModeResult.getCommandNumber());
            deviceLog.setSendUser(Constants.DEVICE_USER);
            deviceLog.setImei(mqModeResult.getImei());
            deviceLog.setBigType(Constants.MESS_TYPE);
            deviceLog.setType(Constants.SET_WORK_MODE);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("设备号", mqModeResult.getImei());
            jsonObject.put("工作模式", mqModeResult.getMode());
            jsonObject.put("时间间隔", mqModeResult.getInterval());
            jsonObject.put("经度", mqModeResult.getMqLocationData().getLongitude());
            jsonObject.put("纬度", mqModeResult.getMqLocationData().getLatitude());
            jsonObject.put("速度", mqModeResult.getMqLocationData().getSpeed());
            jsonObject.put("方位", mqModeResult.getMqLocationData().getOrientation());
            jsonObject.put("日期", mqModeResult.getDeviceDate());
            jsonObject.put("返回指令时间", mqModeResult.getDeviceTime());
            deviceLog.setCmd(jsonObject.toJSONString());
            deviceLog.setSendTime(System.currentTimeMillis());
            deviceLog.setState(1);
            deviceLogService.addDeviceLog(deviceLog);
        } else {
            saveUnInStorage(mqModeResult.getImei());
            receiverUtils.cloaseChannel(mqModeResult.getImei());
        }
    }

}
